#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

#define NUM 16
//const char *filepath="/home/wj/one.-studybylinux/study2023/T2307/T0715/proc02.out";
const char *filepath="./proc02.out";

int main(int argc, char *argv[], char *env[])
{   
    //设置一个环境变量:fork后子进程能继承父进程的环境变量
    char *const _env[NUM]={(char*)"MY_ENV=2233445566",NULL};

    pid_t id=fork();
    if(id<0)
    {
        return -1;
    }
    else if(id==0)//子进程
    {
        printf("子进程开始运行，pid:%d\n",getpid());
        execle(filepath,"proc02.out","-a",NULL,env);
        //execle(filepath,"proc02.out","-a",NULL,_env);
        //execlp(filepath,"proc02.out","-b",NULL);
        exit(-2);//进程替换失败
    }
    else//父进程 
    {
        printf("父进程开始运行，pid:%d\n",getpid());
        //让父进程阻塞式等待子进程：子进程运行完毕，父进程获取后才退出，保证运行时的顺序(PS:单独fork，父子进程顺序具有不确定性)
        int status=0;
        pid_t ret=waitpid(-1,&status,0);//参数：等待任意一个子进程、获取进程状态、阻塞式等待
        if(ret)
            printf("wait success, exit code:%d\n",WEXITSTATUS(status));

    }
    return 0;
}

